//
//  UserViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "UserViewController.h"
#import "CollectionViewController.h"
#import "AboutUsViewController.h"
#import "NightMode.h"
#import "NightManager.h"
#import "BaseTableViewCell.h"
@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSArray *arrayOfTitle;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSString *filePath;
@end

@implementation UserViewController
- (void)dealloc
{
    [_arrayOfTitle release];
    [_tableView release];
    [_filePath release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfTitle = @[@"登陆",@"我的收藏",@"夜间模式",@"清除缓存",@"关于我们"];
        self.filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    // 创建列表
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOfTitle.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[BaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    if (indexPath.row == 3) {
        CGFloat size = [self folderSizeAtPath:self.filePath];
        cell.textLabel.text = [NSString stringWithFormat:@"%@       %.2fMB",self.arrayOfTitle[indexPath.row],size];
    }else{
        cell.textLabel.text = self.arrayOfTitle[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        CollectionViewController *collection = [[CollectionViewController alloc]init];
        [self.navigationController pushViewController:collection animated:YES];
        [collection release];
    }else if (indexPath.row == 4){
        AboutUsViewController *about = [[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:about animated:YES];
        [about release];
    }else if (indexPath.row == 2){
        [NightMode shareNightMode].isNight = ![NightMode shareNightMode].isNight;
        [[NightMode shareNightMode] saveNightModeInfoToDisk];
    }else if (indexPath.row == 3){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认清理吗" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
        [alert show];
        [alert release];
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self clearCache:self.filePath];
    }
    [alertView setMessage:@"清理成功"];
    [alertView show];
    
    [self.tableView reloadData];
}
// 清除缓存
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}
// 计算单个文件大小
- (CGFloat)fileSizeAtPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}
// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
    }
    return folderSize;
}
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.tableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
